<template>
  <div class="components-container">
    <el-form :label-position="labelPosition" label-width="80px" :model="form">
      <el-form-item label="新闻类别">
        <el-select v-model="form.classid" placeholder="请选择新闻类别">
          <el-option
            v-for="item in classData"
            :label="item.text"
            :value="item.value"
            :key="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="新闻标题">
        <el-input
          v-model="form.title"
          maxlength="100"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="新闻简介">
        <el-input
          type="textarea"
          v-model="form.introduction"
          maxlength="200"
          show-word-limit
        ></el-input>
      </el-form-item>
    </el-form>
    <div>
      <p><span>新闻详情编辑区</span></p>
      <tinymce v-model="form.content" :height="300" />
    </div>
    <el-row class="btnBox"
      ><el-button @click="reload()">清空重置</el-button
      ><el-button type="primary" @click="getData">确认提交</el-button></el-row
    >
  </div>
</template>

<script>
import Tinymce from "@/components/Tinymce";

export default {
  name: "TinymceDemo",
  components: { Tinymce },
  data() {
    return {
      labelPosition: "right",
      classData: [
        {
          value: 1,
          text: "媒体报道",
        },
        {
          value: 2,
          text: "社会责任",
        },
        {
          value: 3,
          text: "学术资讯",
        },
      ],
      form: {
        classid: "",
        newsClass: "",
        title: "",
        introduction: "",
        content: "",
      },
    };
  },
  methods: {
    reload() {
      location.reload();
    },
    getData() {
      let { classid, title, introduction } = this.form;
      let content = encodeURIComponent(this.form.content);
      let newsClass = "";
      if (classid == 1) {
        newsClass = "媒体报道";
      } else if (classid == 2) {
        newsClass = "社会责任";
      } else {
        newsClass = "学术资讯";
      }
      let releaseTime = new Date().getTime();
      if (!classid || !title || !introduction || !content) {
        this.$message({
          message: "请填写完整",
          type: "warning",
        });
        return;
      }
      const loading = this.$loading({
        lock: true,
        text: "提交中...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      let newsData = `classid=${classid}&newsClass=${newsClass}&title=${title}&introduction=${introduction}&content=${content}&releaseTime=${releaseTime}`;
      console.log(newsData);
      this.axios.post("news/save_news", newsData).then(res => {
        console.log(res);
        if (res.status == 200) {
          loading.close();
          this.$message({
            message: "已提交成功",
            type: "success",
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.editor-content {
  margin-top: 20px;
}
.btnBox {
  text-align: center;
  > button {
    margin: 20px 30px;
  }
}
</style>
